package com.geeguo.ebuilder.core.feign.config;

import com.geeguo.ebuilder.core.common.action.ActionResultCode;
import com.geeguo.ebuilder.core.common.exception.ApplicationException;
import feign.Response;
import feign.codec.ErrorDecoder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;

/**
 * Feign 错误处理配置
 */
@Slf4j
@Configuration
public class FeignErrorDecoder implements ErrorDecoder {

    @Override
    public Exception decode(String methodKey, Response response) {
        try {
            if (response.request().body() != null) {
                String params = new String(response.request().body());
                log.error("Feign调用失败, method: {} , params: {} .", methodKey, params);
            }
            log.error("Feign调用失败, status: {} , url: {} .", response.status(), response.request().url());
            return new ApplicationException(ActionResultCode.Exception.getMessage());
        } catch (Exception e) {
            log.error("Response转换异常", e);
            return new ApplicationException(ActionResultCode.Exception.getMessage());
        }
    }
}
